using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Core;
using Tester.Forms.GuiActions.Actions;

namespace Tester.GuiActions.Actions
{
    public class Actions
    {

        private static Actions _instance;

        public static Actions Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new Actions();
                    new ActionsReader("Actions.xml").ReadActions(_instance);
                }
                return _instance;
            }
        }

        private MainMenuManager _mainMenuManager;
        private ContextManager  _contextManager;
        private ActionsManager  _actionsManager;
        private AcceleratorManager _acceleratorManager = new AcceleratorManager();

        public MainMenuManager MainMenuManager
        {
            get
            {
                return _mainMenuManager;
            }
        }

        public ContextManager ContextManager
        {
            get
            {
                return _contextManager;
            }
        }


        public ActionsManager ActionsManager
        {
            get
            {
                return _actionsManager;
            }
        }


        public AcceleratorManager AcceleratorManager
        {
            get
            {
                return _acceleratorManager;
            }
        }

        public void ReadMenu(XmlReader reader)
        {
            _mainMenuManager = new MainMenuManager();
            MainMenuManager.MainMenuItem item = _mainMenuManager.RootItem;
            ReadMenu(reader, item);
        }

        private void ReadMenu(XmlReader reader, MainMenuManager.MainMenuItem item)
        {
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        {
                            if (reader.Name == "Item")
                            {
                                string name;
                                string display;
                                reader.MoveToAttribute("name");
                                name = reader.Value;
                                reader.MoveToAttribute("display");
                                display = reader.Value;
                                MainMenuManager.MainMenuItem newItem = new MainMenuManager.MainMenuMenuItem(name, display);
                                _mainMenuManager.AddSubitem(item, newItem);
                                reader.MoveToElement();
                                if (!reader.IsEmptyElement)
                                {
                                    ReadMenu(reader, newItem);
                                }
                            } else if (reader.Name == "Separator")
                            {
                                MainMenuManager.MainMenuItem newItem = new MainMenuManager.MainMenuSeparator();
                                _mainMenuManager.AddSubitem(item, newItem);
                            }
                        }
                        break;
                    case XmlNodeType.EndElement:
                        return;
                    default:
                        break;
                }
            }
        }

        public void ReadContexts(XmlReader reader)
        {
            _contextManager = new ContextManager();
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        {
                            if (reader.Name == "Context")
                            {
                                string name;
                                reader.MoveToAttribute("name");
                                name = reader.Value;
                                _contextManager.AddEmptyContext(name);
                            }
                        }
                        break;
                    case XmlNodeType.EndElement:
                        return;
                    default:
                        break;
                }
            }
        }

        public void ReadActions(XmlReader reader)
        {
            _actionsManager = _actionsManager ?? new ActionsManager();
            string name;
            string className;
            reader.MoveToAttribute("name");
            name = reader.Value;
            reader.MoveToAttribute("class");
            className = reader.Value;
            reader.GetAttribute("name");
            _actionsManager.AddAction(reader, name , className);            
        }


    }
}
